package com.caifu.controller;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.caifu.bean.Result;
import com.caifu.bean.ResultUtil;
import com.caifu.pojo.ZcMealAllocation;
import com.caifu.service.ZcMealAllocationService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.Date;


/**
 * <p>
 * 餐费配置表 前端控制器
 * </p>
 *
 * @author szt
 * @since 2020-11-09
 */
@Controller
@RequestMapping("/mealAllocation")
public class ZcMealAllocationController extends AbstractController {

    @Autowired
    private ZcMealAllocationService mealAllocationService;

    @GetMapping("/index")
    public String index(ModelMap map) {
        map.put("meal",mealAllocationService.getOne(new QueryWrapper<ZcMealAllocation>().orderByDesc("update_time").orderByDesc("create_time")));
        return "foodDelivery/mealAllocation/index";
    }

    @PostMapping("save")
    @ResponseBody
    public Result save(String meals,String deliveryFee){
        try{
            ZcMealAllocation meal = mealAllocationService.getOne(new QueryWrapper<ZcMealAllocation>().orderByDesc("update_time").orderByDesc("create_time"));
            if(meal != null){
               meal.setMeals(meals);
               meal.setDeliveryFee(deliveryFee);
               meal.setUpdateBy(getUserNo()+"");
               meal.setUpdateTime(new Date());
               mealAllocationService.updateById(meal);
            }else{
                meal = new ZcMealAllocation();
                meal.setMeals(meals);
                meal.setDeliveryFee(deliveryFee);
                meal.setCreateBy(getUserNo()+"");
                meal.setCreateTime(new Date());
                mealAllocationService.save(meal);
            }
            return new Result(200,"保存成功！",null);
        }catch (Exception e){
            e.printStackTrace();
            return ResultUtil.error(-1,"保存失败！");
        }
    }
}
